﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace SharedGenomics.Domain
{
    public class PhenotypeFileDataSchema : DataSchema
    {
        #region Public Methods

        public ICollection<string> GetCandidateIdColumnNames()
        {
            return  this.Where(col => col.IsCandidateKey)
                        .Select(col => col.Name).ToArray();
        }

        public ICollection<string> GetCandidateSexColumnNames()
        {
            return this.Where(col => this.IsCandidateSexColumn(col))
                        .Select(col => col.Name).ToArray();
        }

        public void SetIdentifierColumn(string columnName)
        {
            if (columnName == null)
                throw new ArgumentNullException();

            DataColumnTypeBase column = this[columnName];

            if (column.IsCandidateKey)
            {
                this.IdColumnName = columnName;
            }
            else
            {
                throw new ArgumentException(String.Format("Column '{0}' is not a candidate key column", columnName));
            }
        }

        public void SetSexColumn(string columnName, string maleValue, string femaleValue)
        {
            if (columnName == null)
                throw new ArgumentNullException("columnName");

            DataColumnTypeBase column = this[columnName];

            if (this.IsCandidateSexColumn(column))
            {
                //create map for sex on given column
                column.CreateMap<Sex>(  new KeyValuePair<string, Sex>(maleValue, Sex.Male),
                                        new KeyValuePair<string, Sex>(femaleValue, Sex.Female));

                this.SexColumnName = columnName;
            }
            else
            {
                throw new ArgumentException(String.Format("Column '{0}' is not a candidate sex column", columnName));
            }
        }

        #endregion

        #region Private Methods

        private bool IsCandidateSexColumn(DataColumnTypeBase column)
        {
            Debug.Assert(column != null);

            return column.ValueCount == 2;
        }

        #endregion

        #region Properties

        public string IdColumnName { get; private set; }
        public string SexColumnName { get; private set; }

        #endregion
    }
}
